﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Pdelvo.Minecraft.Network;

namespace Pdelvo.Minecraft.Protocol.Packets
{
    [RequireVersion(31, true)]
    public class EncryptionKeyRequest : Packet
    {
        public string ServerID { get; set; }
        public byte[] PublicKey { get; set; }

           /// <summary>
        /// Initializes a new instance of the <see cref="EncryptionKeyRequest"/> class.
        /// </summary>
        /// <remarks></remarks>
        public EncryptionKeyRequest()
        {
            Code = 0xFD;
        }

        /// <summary>
        /// Receives the specified reader.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="version">The version.</param>
        /// <remarks></remarks>
        public override void Receive(BigEndianStream reader, int version)
        {
            ServerID = reader.ReadString16(100);
            PublicKey = reader.ReadBytes(reader.ReadShort());
        }

        /// <summary>
        /// Sends the specified writer.
        /// </summary>
        /// <param name="writer">The writer.</param>
        /// <param name="version">The version.</param>
        /// <remarks></remarks>
        protected override void Send(BigEndianStream writer, int version)
        {
            writer.Write(Code);
            writer.Write(ServerID);
            if(PublicKey == null)
                PublicKey = new byte[0];
            writer.Write((short)PublicKey.Length);
            writer.Write(PublicKey);
        }
    }
}
